// pages/biz/activity/apply/apply.js
import applyServer from './applyServer'
var regeneratorRuntime = require("../../../../../lib/js/runtime-module.js")
import Config from '../../../../../utils/config2.js'
var WxParse = require('../../../../../wxParse/wxParse.js');
var utils = require("../../../../../utils/util.js");
import Util from '../../../../../utils/util2.js';
const app = getApp();
let userInfoData = wx.getStorageSync('userInfoData')
// console.log(userInfoData)
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userName: '',
    mobile: userInfoData.mobile||'',
    personNum: '',
    name: userInfoData.name||'',
    amount: '1',
    minusStatus: false,
    initList: {},
    backMsg: '', //返回状态
    activityId: '', //活动id
    productId: '', //美房Id
    tips: '', //提示信息
    content:'',
    show:false
  },
  // 表单输入
  bindinput: function(e) {
    let label = e.currentTarget.dataset.label
    let value = e.detail.value
    if (label == 'name') {
      let name = 'form.' + e.target.dataset.name　　
      let val = e.detail.value
      
        this.setData({
          name: value,
        })

    } else if (label == 'mobile') {
      this.setData({
        mobile: value,
      })
    } else if (label == 'personNum') {
      this.setData({
        personNum: value,
      })
    }
  },
  //点击提交
  submit: function(e) {

    let msgList = this.data
    console.log(this.data, 'this.data')
    let reg = /^[1][3,4,5,7,8,9][0-9]{9}$/
    let regName = /^[\u4e00-\u9fa5_a-zA-Z0-9]+$/;
    if (msgList.name.trim() == '') { // 新手机号判空
      wx.showToast({
        title: '姓名不能为空',
        icon: 'none'
      })
      return
    }
    console.log(msgList.amount)
    if (msgList.amount.toString().trim() == '') { // 参与人数不能判空
      wx.showToast({
        title: '参与人数不能为空',
        icon: 'none'
      })
      return
    }
    if (msgList.amount.toString().trim() == 0) { // 参数人数不能为0
      wx.showToast({
        title: '参数人数不能为0',
        icon: 'none'
      })
      return
    }
    if (!regName.test(msgList.name)){
      wx.showToast({
        title: '姓名不能有特殊符号',
        icon: 'none'
      })
      return
    }
    if (msgList.mobile.trim() == '') { // 旧手机号判空
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none'
      })
      return
    }
    if (msgList.mobile.length != 11 || !reg.test(msgList.mobile)) { // 11位
      wx.showToast({
        title: '新手机号输入错误，请重新输入',
        icon: 'none'
      })
      return
    }
    let params = {
      activityId: this.data.activityId,
      amount: this.data.amount,
      mobile: this.data.mobile,
      name: this.data.name,
    }
    let projectName = this.data.initList.projectName
    if(this.data.eId){
      utils.addBehaviorTrail(this.data.eId, 58, '',projectName +'活动',params.name,params.amount);
    }
    this.getApplyList(params) //调用报名接口
    
  },
  /**获取报名信息 */
  async getApplyList(params) {
    params.appId = app.globalData.appId;
    wx.showLoading({
      title: '正在报名中',
    })
    let data = await applyServer.getApply(params)
    if(data.success){
      this.setData({
        backMsg: data.success,
      })
      wx.hideLoading()
    }else{
      this.setData({
        tips: data.msg
      })
      wx.hideLoading()
    }
    this.setData({
      eId:  wx.getStorageSync("eId") || "",
    });
    
    setTimeout(() => {
      if (this.data.backMsg == true) {
        wx.navigateTo({
          url: `../applyResult/applyResult?name=${this.data.name}&mobile=${this.data.mobile}&amount=${this.data.amount}&productId=${this.data.productId}`
        })
      } else {
        console.log(111)
        wx.showToast({
          title: this.data.tips,
          icon: 'none'
        })
      }
    }, 500);
  },

  //点击减号
  bindMinus() {
    var amount = this.data.amount
    if (amount > 1) {
      amount--;
    }
    var minusStatus = amount <= 1 ? true : false;
    this.setData({
      amount: amount,
      minusStatus
    })
  },
  //点击加号
  bindPlus() {
    var amount = this.data.amount;
    if (amount < 99){
      amount++;
    }else{
      wx.showToast({
        title: '最多参与人数为99人！',
        icon:'none'
      })
    }
    var minusStatus = (amount == 3 ? true : false);
    this.setData({
      amount: amount,
      minusStatus
    })
  },
  //输入框事件
  bindManual(e) {
    var amount = e.detail.value;
    this.setData({
      amount: amount
    })

  },
  //初始化接口
  async initInfo(params) {
    let initList = await applyServer.getInit(params)
    let data = initList.data
    console.log(initList, 'initList')
    if(data){
      WxParse.wxParse('content', 'html', data.detail, this, 5)
    }
    if (data.startDate){
      data.startDate = data.startDate.replace(/-/g, '.')
    }
    // if (data.endDate) {
    //   data.endDate = data.endDate.replace(/-/g, '.')
    // }
    if (initList.success == true) {
      this.setData({
        initList: data,
      })
      if (data.endDate){
        let date = Util.dateFromString(data.endDate);
         date = date.getTime();
         let date1=Util.dateFromString()
        date1 = date1.getTime();
        console.log(date,date1)
        if(date>date1){
          this.setData({
            show:true
          })
        }
      }
      // this.dat
      wx.setNavigationBarTitle({
        title: data.projectName+'活动',
      })
      let eId = wx.getStorageSync('eId')
      if(eId){
        utils.addBehaviorTrail(eId,61,'',`【活动】${data.spreadName}`)
      }
    }
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {   
    let token = wx.getStorageSync('authName');
    let mobile = wx.getStorageSync('phoneNo');
    let contentId = options.contentId;
    let newsDetailUrl = `${Config.host}${Config.newsDetailUrl}?host=${Config.host}&id=${contentId}&token=${token}`;
    this.setData({ 
      newsDetailUrl,
      eId:  wx.getStorageSync("eId") || ""
     })

    console.log(options, 'options--------------------------')
    this.setData({
      activityId: options.contentId,
      productId: options.productId
    })
    
    let params = {
      contentId: options.contentId,mobile
    } //这个id是美房id
    this.initInfo(params)
    
  },
  onShow:function(){
    wx.showShareMenu({
      withShareTicket: true
    })
    // if (!wx.getStorageSync(Config.authName)) {
    //   wx.redirectTo({
    //     url: '/pages/biz/login/login',
    //   })
    // }
  }
  
})